<?php

namespace app\store\service;

use app\store\model\Coupon;
use think\facade\Db;

class ComboService
{
    public function getComboList()
    {
        $store_id = session('store_id');
        $comboList = Db::name('combo')
                       ->field('id,user_grade_id,combo_image,combo_name,origin_price,sell_price,status,create_time,update_time')
                       ->where('store_id',$store_id)
                       ->withAttr('create_time',function ($value){
                           return date('Y-m-d H:i:s',$value);
                       })->withAttr('update_time',function ($value){
                            return date('Y-m-d H:i:s',$value);
                       })->where('delete_time',0)->select();

        return $comboList;
    }

    /**
     * 获取套餐选项
     * */
    public static function getComboOptions()
    {
        $store_id = session('store_id');
        $comboOptions = Db::name('combo')
                           ->field('id,combo_name')
                           ->where(['store_id' => $store_id,'status' => 1])
                           ->select();

        return $comboOptions;
    }


    public static function getCouponOptions($param)
    {
        $store_id = session('store_id');

        $model = new Coupon();
         $query= $model->field('id,icon,coupon_name,expire_type,quota,start_time,end_time,expire_day,coupon_type')
                ->withAttr('coupon_type',function ($value){
                $type = [1 => '满减卷',2 => '立减劵',3 => '拼团卷', 4 => '转卖卷'];
                return $type[$value];
            })->where('store_id',$store_id)
            ->order('id','desc');

       if (!empty($param['coupon_type'])){
          $couponOptions =  $query->where('coupon_type',$param['coupon_type'])->paginate(['list_rows' => 10,'query' => ['coupon_type' => $param['coupon_type']]]);

          return  $couponOptions;
       }


       $couponOptions = $query->paginate(['list_rows' => 10]);

        return $couponOptions;
    }


    public function saveCombo($data)
    {
        $store_id = session('store_id');
       $result =  Db::name('combo')->insert([
                'combo_name' => $data['combo_name'],
                'store_id' => $store_id,
                'combo_image' => $data['combo_image'],
                'origin_price' => $data['origin_price'],
                'sell_price' => $data['sell_price'],
                'combo_desc' => $data['combo_desc'],
                // 'birthday_give_integral' => $data['birthday_give_integral'],
                'give_self_month_num'=>$data['give_self_month_num'],
                'give_self_coupon_count' => $data['give_self_coupon_count'],
                 'user_grade_id' => $data['user_grade_id'],
                'give_integral' => $data['give_integral'],
                'give_coupon' => $data['give_coupon'],
                'give_balance' => $data['give_balance'],
                'status' => $data['status'],
                'create_time' => time(),
                'update_time' => time(),
        ]);

       return $result;
    }

    public function findCombo($id)
    {
        $combo = Db::name('combo')
                ->field('id,combo_name,combo_image,give_self_coupon_count,give_self_month_num,origin_price,sell_price,status,give_coupon,give_balance,give_integral,combo_desc,user_grade_id')
                ->where('id',$id)
                ->find();


        $coupons = [];

        if (!empty($combo['give_coupon'])){
            $give_coupon = json_decode($combo['give_coupon'],true);
            $ids = array_column($give_coupon,'coupon_id');
            $couponList = Db::name('coupon')
                ->field('id,icon,coupon_name')
                ->whereIn('id',$ids)
                ->select();


            foreach ($couponList as $k => $v){
                $coupons[$k]['coupon_id'] = $v['id'];
                $coupons[$k]['coupon_icon'] = $v['icon'];
                $coupons[$k]['coupon_name'] = $v['coupon_name'];
                $coupons[$k]['give_num'] = $give_coupon[$k]['give_num'];
            }
            $combo['coupons_info'] = $coupons;
        }





        return $combo;
    }

    public function updateCombo($data)
    {
        $result =  Db::name('combo')
                      ->where('id',$data['id'])
                      ->update([
                          'combo_name' => $data['combo_name'],
                          'user_grade_id' => $data['user_grade_id'],
                          'combo_image' => $data['combo_image'],
                          'origin_price' => $data['origin_price'],
                          'sell_price' => $data['sell_price'],
                          'combo_desc' => $data['combo_desc'],
                          'give_integral' => $data['give_integral'],
                          'give_coupon' => $data['give_coupon'],
                          'give_balance' => $data['give_balance'],
                          'status' => $data['status'],
                        //   'birthday_give_integral'=>$data['birthday_give_integral'],
                          'give_self_coupon_count'=>$data['give_self_coupon_count'],
                          'give_self_month_num' => $data['give_self_month_num'],
                          'update_time' => time(),
                    ]);

        return $result;
    }

    public function deleteCombo($id)
    {
        $result =  Db::name('combo')
                     ->where('id',$id)
                     ->update(['delete_time' => time()]);

        return $result;
    }
}